/* **************************************************************************/
/*COPYRIGHT CCWIN-IN ALL RIGHTS RESERVED. THIS IS AN UNPUBLISHED WORK.      */
/*V8:ConvertMode=Report                                                     */
/*V8:WebEnabled=Yes                                                         */
/*V8:RunMode=Character,Windows                                              */
/*EXTRACTING DATA FROM MFG/PRO                                              */
/****************************************************************************/
/* PROCEDURE NAME        : xxcustomershippercfmapi.p                        */
/* PROCEDURE TYPE        : JSON API Process                                 */
/* DESCRIPTION           : Customer shipper confirm CIM-LOAD Process        */
/*                                                                          */     
/* INCLUDE FILES         :                                                  */
/* CALLED BY             :                                                  */
/* CALLED PROCEDURES     :                                                  */
/****************************************************************************/
/* CREATED BY            : Gaoyan                               31-Mar-24   */
/****************************************************************************/ 
/****************************************************************************/
/****************************************************************************/

{us/mf/mfdtitle.i}
{us/bbi/gplabel.i}
{us/wb/wbrp01.i}
{xxjson.i}
{xxcimload.i}

&SCOPED-DEFINE TimeStamp1 substring(string(year(today), "9999"),3) + string(month(today), "99") + string(day(today), "99")
&SCOPED-DEFINE TimeStamp2 substring(string(time, "HH:MM:SS"), 1, 2) + substring(string(time, "HH:MM:SS"), 4, 2) + substring(string(time, "HH:MM:SS"), 7, 2)

define input parameter vc_inputjson             as character format "x(60)" no-undo.
define input parameter vc_outputjson            as character format "x(60)" no-undo.
define input parameter vc_import                as logical  no-undo.


DEFINE VARIABLE li_access           AS    INTEGER                       NO-UNDO.
define variable l_file                       as character format "x(40)" no-undo.
define variable l_line                       as character   no-undo.
define variable lvi_row_id                   as decimal     no-undo.
define variable v_import                     as logical     init   "no"      no-undo.
define variable v_site                       like si_site   no-undo.

define variable l_error                      as character      no-undo.
define stream f_log.
define stream f_cim.
define variable v_quote               as character initial """"   no-undo.
define variable v_default             as character initial "-"    no-undo.
define variable v_space               as character initial " "    no-undo.
define variable v_log                 as character                no-undo.
define variable v_cim                 as character                no-undo.
define variable v_fline               as character                no-undo.
define variable v_errorstatus         as logical                  no-undo.
define variable v_toterrorstatus         as logical                  no-undo.
define variable v_errstring           as character                no-undo.
define variable v_status              as character                no-undo.

define variable v_entity              like si_entity              no-undo.
define variable v_ee                  as logic                    no-undo.

define variable vi_filecount          as integer                  no-undo.
define variable vc_filename           as character                no-undo.
define variable vc_topath             as character                no-undo.
define variable v_runproagram        as character                no-undo.
define variable vc_traceid            as character                no-undo.


            
define temp-table ttcustomerasn no-undo
        
       field  abs_customer               like abs_mstr.abs_shipfrom
       field  abs_asn_nbr                like abs_mstr.abs_id
       field  abs_site                   like abs_mstr.abs_shipto 
       field  abs_shipto                 like abs_mstr.abs_shipto 
       field  abs_inv_movment_code       like abs_mstr.abs_shipto 
       field  abs_shipfrom               like abs_mstr.abs_shipto 
       field  abs_loc                    like   abs_mstr.abs_loc 
       field  abs_ship_date              like abs_mstr.abs_shp_date
       field  abs_arrive_date            like abs_mstr.abs_shp_date
       field  abs_due_date               like abs_mstr.abs_shp_date
       field  abs_effective_date         like abs_mstr.abs_shp_date
       field  abs_part                   like scx_ref.scx_part
       field  abs_cust_part              like scx_ref.scx_part
       field  abs_po_nbr                 as   character
       field  abs_model_yr               as   character
       field  abs_order                  like po_nbr
       field  abs_line                   like scx_ref.scx_line
       field  abs_qty                    as  decimal
       field  abs_um                     like pt_mstr.pt_um
       field  abs_conv                   as  decimal
       field  abs_traceid                as   character
       field  abs_dataid                 as   character
        .

      DEFINE DATASET dsttcustomerasn FOR ttcustomerasn.

      hdataset = dataset dsttcustomerasn:HANDLE.    


mainloop:
repeat on error undo mainloop, retry mainloop:

   /* Output and Batch Input */
   
   {us/xx/xxworkpath_e.i &outbound=YES &inbound=no &archive=YES}

assign m_outpath     = m_outpath + "asn/"
       m_archpath    = m_archpath + "asn/"
       m_successpath = m_successpath + "asn/"
       m_errorpath   = m_errorpath + "asn/"
       m_workpath    = m_workpath + "asn/".


   empty temp-table ttcustomerasn.

      /* Load item master data from JSON  */ 
      vc_outputjson = replace(vc_inputjson,".json",".run.out.json").   
      run loadjsonfile(input vc_inputjson,
                       input vc_outputjson,
                       output vl_RetOK).
      if vl_RetOK then
      do:
         find first ttcustomerasn no-error.
         assign vc_traceid = ttcustomerasn.abs_traceid.
      end.

  /* Create CIM-LOAD file and do cim-load */
  v_cim = string(vc_traceid).
  v_log = string(vc_traceid).
  
 output to /home/mfg/work/xxcustomerasnapi.log  .      
 
if vc_import and vl_RetOK then do:  

  /* create cim load format */
                
  cimloadprocess:
  do transaction on error undo cimloadprocess, leave cimloadprocess: 
  
  for each ttcustomerasn  break by ttcustomerasn.abs_asn_nbr.

   if first-of(ttcustomerasn.abs_asn_nbr) then do:

    v_cim = m_workpath + "ASNCNF" + "_" + string(ttcustomerasn.abs_traceid) + "_" + string(ttcustomerasn.abs_asn_nbr) + "_" + {&TimeStamp1} + "_" + {&TimeStamp2}  + ".cim".
    v_log = m_workpath + "ASNCNF" + "_" + string(ttcustomerasn.abs_traceid) + "_" + string(ttcustomerasn.abs_asn_nbr) + "_" + {&TimeStamp1} + "_" + {&TimeStamp2}  + ".log".
    
    output stream f_cim to value(v_cim).
    
    find first abs_mstr no-lock where abs_domain = global_domain
                            and substring(abs_mstr.abs__qad01,41,20) = ttcustomerasn.abs_asn_nbr
                            and abs_type = "S"
                            and abs_site = ttcustomerasn.abs_site
                            no-error.
                
   if avail abs_mstr then 
    put stream f_cim  unformatted 
            v_quote ttcustomerasn.abs_shipfrom v_quote v_space                    /*  Ship from */
            v_quote "S" v_quote  v_space                                          /*  pre-shipper shipper */
            v_quote substring(abs_mstr.abs_id,2,50)  v_quote skip                       /*  ASN */
            v_quote ttcustomerasn.abs_ship_date  v_quote skip                     /*  ship date */
            v_quote ttcustomerasn.abs_effective_date v_quote  v_space   skip      /*  effective date  */
            v_default v_space       skip                                          /* document */
            v_default v_space                                                     /* abs_veh_ref */
            v_default v_space                                                     /* ship time */
            v_default v_space                                                     /* arrive date  */
            v_default v_space  skip                                               /* arrive time*/
            "N" v_space                                                           /* post invoice */
            "N" v_space                                                           /* print detail */
            "N" v_space                                                           /* print invoice */
            "N" v_space                                                           /* combine invoice */
            "N" v_space  skip                                                         /* freight */
            "Y" v_space  skip .                                                    /* correct */
            
       else 
       do:
       create api_status.
                  assign api_status.data_seq     = 1
                         api_status.return_status  = "FAILED"
                         api_status.message_type = "Pre-validation"
                         api_status.message_string  =    ttcustomerasn.abs_asn_nbr + " " 
                                                      + GetTermLabel("asn",6)
                                                      + GetTermLabel("DOES_NOT_EXIST",6)
                         api_status.dataid  =  ttcustomerasn.abs_dataid
                         api_status.billnbr = ttcustomerasn.abs_asn_nbr
                         api_status.traceid = vc_traceid 
                         api_status.domain = global_domain.  
       
       end.
         
     end. /* if first-of(ttcustomerasn.abs_asn_nbr) then do: */ 
    
	   
   if last-of(ttcustomerasn.abs_asn_nbr) then do:
   
                                
    output stream f_cim close.
    
         
     /* cim load process */
     v_runproagram = "rcsois.p".
     execname = "xxcustomershippercfmapi.p".

     
       put unformatted v_cim " " skip
                       v_log " " skip 
                       m_outpath " " skip
                       m_archpath " " skip
                       v_runproagram " " skip
                       v_errstring " " skip 
                       v_errorstatus skip .
                       
       run cimloadprocess(input v_cim,
                          input v_log,
                          input v_runproagram,
                          input vc_traceid,
                          input ttcustomerasn.abs_dataid,
                          input ttcustomerasn.abs_asn_nbr,
                          output v_errstring,
                          output v_errorstatus) .
                          
       v_toterrorstatus = v_toterrorstatus and v_errorstatus.    


            /* Handle cim and log file */
        do vi_filecount = 1 to 2:
          /* when success move cim log file to archive directory  */
          /* when failed move cim log file to error directory  */   
          /*  move status json file to out directory for API return */   
          /* when success move previous step .p and json file to archive directory  */
          /* when failed move previous step .p and json file to error directory  */       
          case vi_filecount:
          when 1 then
          assign vc_filename = v_cim
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath.
          when 2 then
          assign vc_filename = v_log
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath.
           end case.           
          run cimfilehandle(input vc_filename,
                            input vc_topath,
                            input no ).                 
                 
       end. /* do vi_filecount = 1 to 2: */   
     end. /* if last-of */
     
    end. /* for each ttptp  */

     if  v_toterrorstatus then 

       undo cimloadprocess, leave cimloadprocess.          
            
        
    end.    /* do transaction on error undo cimloadprocess, leave cimloadprocess: */

       if can-find(first api_status where api_status.return_status = "FAILED" ) then
       do:
       
       for each api_status where api_status.return_status = "SUCCESS".
         
         delete api_status.
         
       end.
       
       end.  /* if can-find(first api_status */
         


   end. /* if vc_import */
   
        /* Handle cimload related files */
         /* Create output json */                   
         run writejsonfile(input hresult,
                  input vc_outputjson,
                  input "FILE",
                  input True,
                  input "UTF-8",
                  output vl_RetOK,
                  output vc_errorstring).
                  
         put unformatted v_cim " " skip
                         v_log " " skip 
                         m_outpath " " skip
                         m_archpath " " skip
                         v_runproagram " " skip
                         v_errstring " " skip 
                         v_errorstatus skip .
       do vi_filecount = 1 to 5:
          /* when success move cim log file to archive directory  */
          /* when failed move cim log file to error directory  */   
          /*  move status json file to out directory for API return */   
          /* when success move previous step .p and json file to archive directory  */
          /* when failed move previous step .p and json file to error directory  */       
          case vi_filecount:
      
          when 1 then
          assign vc_filename = replace(vc_inputjson,".json",".p")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 2 then
          assign vc_filename = vc_inputjson
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 3 then
          assign vc_filename = replace(vc_inputjson,".json",".dat")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 4 then
          assign vc_filename = replace(vc_inputjson,".json",".run.log")
                 vc_topath   = m_outpath.
          when 5 then
          assign vc_filename = replace(vc_inputjson,".json",".run.out.json")
                 vc_topath   =  m_outpath.
          end case.
          
         if vl_RetOK then 
         put unformatted vc_filename " " skip
         vc_topath " " skip 
         vi_filecount skip.
     
        
          
          run cimfilehandle(input vc_filename,
                            input vc_topath,
                            input no ).                 
                   
          
     
       end.       /* do vi_filecount = 1 to 5: */        
       
       output close.   
   
   
   leave.
end. /* repeat on error undo mainloop, retry mainloop: */
